#include "datatypedictionary.h"

DatatypeDictionary::DatatypeDictionary(QObject *parent) : QAbstractListModel(parent)
{
    this->insert("http://schema.omg.org/spec/UML/2.1/uml.xml#Integer", "int", true);
    this->insert("http://schema.omg.org/spec/UML/2.1/uml.xml#Any", "any", true);
    this->insert("http://schema.omg.org/spec/UML/2.1/uml.xml#Bool", "bool", true);
    this->insert("http://schema.omg.org/spec/UML/2.1/uml.xml#Char", "char", true);
    this->insert("http://schema.omg.org/spec/UML/2.1/uml.xml#UChar", "uchar", true);
    this->insert("http://schema.omg.org/spec/UML/2.1/uml.xml#Byte", "byte", true);
    this->insert("http://schema.omg.org/spec/UML/2.1/uml.xml#Short", "short", true);
    this->insert("http://schema.omg.org/spec/UML/2.1/uml.xml#UShort", "ushort", true);
    this->insert("http://schema.omg.org/spec/UML/2.1/uml.xml#String", "string", true);
}

DatatypeDictionary::~DatatypeDictionary()
{
    qDebug() << "deleting datatypeDictionary";
}

void DatatypeDictionary::insert(QString id, QString label, bool isPrimitive)
{
    if (!this->datatypeById(id).isValid()) {
        this->_list.append(XmiDatatype(id, label, isPrimitive));
    }
}

void DatatypeDictionary::removeByLabel(QString label)
{
    for (int i = 0; i < this->_list.count(); i++) {
        XmiDatatype dataType = this->_list.at(i);
        if (dataType.label() == label) {
            this->_list.removeAt(i);
            break;
        }
    }
}

XmiDatatype DatatypeDictionary::dataTypeByLabel(QString label) const
{
    foreach (XmiDatatype d, this->_list) {
        if (d.label() == label) {
            return d;
        }
    }

    return XmiDatatype();
}

XmiDatatype DatatypeDictionary::datatypeById(QString id)
{
    foreach (XmiDatatype d, this->_list) {
        if (d.id() == id) {
            return d;
        }
    }

    return XmiDatatype();
}

QString DatatypeDictionary::labelById(QString id)
{
    foreach (XmiDatatype d, this->_list) {
        qDebug() << d.id() << "==" << id;
        if (d.id() == id) {
            return d.label();
        }
    }

    return "";
}

QList<XmiDatatype> DatatypeDictionary::datatypeList() const
{
    return this->_list;
}

bool DatatypeDictionary::containsLabel(QString label)
{
    foreach (XmiDatatype datatype, this->_list) {
        if (datatype.label() == label) {
            return true;
        }
    }

    return false;
}

QVariant DatatypeDictionary::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) {
        return QVariant();
    }

    if (role == Qt::DisplayRole || role == Qt::EditRole) {
        XmiDatatype datatype = this->_list.at(index.row());
        return datatype.label();
    }

    return QVariant();
}

int DatatypeDictionary::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);

    return this->_list.count();
}

int DatatypeDictionary::indexByLabel(QString label)
{
    for (int i = 0; i < this->_list.count(); i++) {
        XmiDatatype datatype = this->_list.at(i);
        if (datatype.label() == label) {
            return i;
        }
    }

    return -1;
}
